Import mojo
Import nDrawExts

#If TARGET="glfw"
	Alias SetBlend = SetBlend2
	'Alias Cls = Cls2
#End 

Function Main:Int()
	New Game()
End Function

Class Game Extends App
	Field img:Image
	Field r:Float 'rotation
	
	Field firstRender:Bool = True
	Field pb:Int[] 'Pixel buffer
	
	Field toggle:Int
	
	Method OnCreate:Int()
		SetUpdateRate 60
		
		img = CreateImage(256, 256,, Image.MidHandle)
		pb = pb.Resize(img.Width * img.Height)
	End Method
	
	Method OnUpdate:Int()
		r += 3
		
		If KeyHit(KEY_ESCAPE) or KeyHit(KEY_CLOSE) Then Error("")
		If KeyDown(KEY_SPACE) Then firstRender = True
	End Method
	
	Method OnRender:Int()
		If firstRender
			If toggle = 0 Then Cls(0, 0, 0) Else Cls2(0, 0, 0, 0) 'Clear color!
			DrawEllipse(128, 128, 64, 32)
			ReadPixels(pb, 0, 0, 256, 256)
			img.WritePixels(pb, 0, 0, 256, 256)
			firstRender = False
			
			toggle = 1 - toggle
		End If

		Cls2(0, 0, 255)
		
		For Local h:Int = 0 To DeviceHeight() Step 64
			SetColor(0, 128, 255)
			DrawRect(0, h + 32 * Sin(r), DeviceWidth(), 32)
			SetColor(255, 255, 255)
		Next
		
		DrawImage(img, MouseX(), MouseY(), r, 1, 1)

	End Method
	
End Class